package com.th.supcom.pds.phamapply.client.widget.inpharmacyreturn;

import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailModel;

/**
 * 门诊退药时间监听
 * 
 * @author yzhang
 */
public class InPharmacyReturnListener
{
    private InPharmacyReturnFields m_inPharmacyReturnFields;
    private InPharmacyReturnButtons m_inPharmacyReturnButtons;
    private InPharmacyReturnAction m_inPharmacyReturnAction;
    private InPharmacyReturnMasterGridPanel m_inPharmacyReturnMasterGridPanel;
    /** 需要监听的全局面板. */
    private Component m_keyNavComponent;

    /**
     * 初始化所有的Listener
     */
    public void initListeners ()
    {
        addButtonListener ();
        addEnterKeyListener ();
        addFocusListeners ();
    }

    /**
     * 界面按钮监听事件
     */
    private void addButtonListener ()
    {
        m_inPharmacyReturnButtons.getQueryBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_inPharmacyReturnAction.findInpPhamApplyDetailByApplyNo (m_inPharmacyReturnButtons.getConfirmBtn (),
                                                                          m_inPharmacyReturnFields.getPrescNoField ());
            }
        });
        m_inPharmacyReturnButtons.getConfirmBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_inPharmacyReturnAction.drugReturn (m_inPharmacyReturnFields.getPrescNoField (),
                                                     m_inPharmacyReturnButtons.getConfirmBtn ());
            }
        });
        m_inPharmacyReturnButtons.getClearBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_inPharmacyReturnAction.clear (true);
                m_inPharmacyReturnFields.getPrescNoField ().focus ();
            }
        });
        m_inPharmacyReturnFields.getPrescNoField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeysConstants.VK_ENTER)
                {
                    m_inPharmacyReturnAction.findInpPhamApplyDetailByApplyNo (m_inPharmacyReturnButtons.getConfirmBtn (),
                                                                              m_inPharmacyReturnFields.getPrescNoField ());
                }
            }
        });
        m_inPharmacyReturnFields.getPatiIdField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeysConstants.VK_ENTER)
                {
                    m_inPharmacyReturnAction.findInpPhamApplyDetailByApplyNo (m_inPharmacyReturnButtons.getConfirmBtn (),
                                                                              m_inPharmacyReturnFields.getPrescNoField ());
                }
            }
        });
        m_inPharmacyReturnMasterGridPanel.getCheckmodel ()
                                         .addSelectionChangedListener (new SelectionChangedListener <InpPhamApplyDetailModel> ()
                                         {
                                             @Override
                                             public void selectionChanged (SelectionChangedEvent <InpPhamApplyDetailModel> se)
                                             {
                                                 if (se == null)
                                                 {
                                                     return;
                                                 }
                                                 List <InpPhamApplyDetailModel> model = se.getSelection ();
                                                 m_inPharmacyReturnAction.doSelectedChange (model);
                                             }
                                         });
    }

    /**
     * 监听快捷键
     */
    private void addEnterKeyListener ()
    {
        new KeyNav <ComponentEvent> (m_keyNavComponent)
        {
            @Override
            public void onKeyPress (ComponentEvent ce)
            {
                int keyCode = ce.getKeyCode ();
                if (ce.isAltKey ())
                {
                    executAltEvent (keyCode);
                }

            }
        };
    }

    /**
     * 执行Alt+?快捷键
     */
    private void executAltEvent (int keyCode)
    {
        if (keyCode == KeysConstants.VK_Q)
        {
            m_inPharmacyReturnAction.findInpPhamApplyDetailByApplyNo (m_inPharmacyReturnButtons.getConfirmBtn (),
                                                                      m_inPharmacyReturnFields.getPrescNoField ());
            return;
        }

        if (keyCode == KeysConstants.VK_O)
        {
            m_inPharmacyReturnAction.drugReturn (m_inPharmacyReturnFields.getPrescNoField (),
                                                 m_inPharmacyReturnButtons.getConfirmBtn ());
            return;
        }
        if (keyCode == KeysConstants.VK_R)
        {
            m_inPharmacyReturnAction.clear (true);
            return;
        }
    }

    /**
     * 监听焦点事件
     */
    private void addFocusListeners ()
    {
        ClientUtils.getFocusChange (m_inPharmacyReturnFields.getPrescNoField (), m_inPharmacyReturnButtons.getConfirmBtn (),
                                    m_inPharmacyReturnButtons.getQueryBtn ());
        ClientUtils.getFocusChange (m_inPharmacyReturnButtons.getQueryBtn (), m_inPharmacyReturnButtons.getClearBtn (),
                                    m_inPharmacyReturnButtons.getConfirmBtn ());
        ClientUtils.getFocusChange (m_inPharmacyReturnButtons.getConfirmBtn (), null, m_inPharmacyReturnButtons.getClearBtn ());
    }

    public void setInPharmacyReturnFields (InPharmacyReturnFields inPharmacyReturnFields)
    {
        m_inPharmacyReturnFields = inPharmacyReturnFields;
    }

    public void setInPharmacyReturnButtons (InPharmacyReturnButtons inPharmacyReturnButtons)
    {
        m_inPharmacyReturnButtons = inPharmacyReturnButtons;
    }

    public void setInPharmacyReturnAction (InPharmacyReturnAction inPharmacyReturnAction)
    {
        m_inPharmacyReturnAction = inPharmacyReturnAction;
    }

    public void setKeyNavComponent (Component keyNavComponent)
    {
        m_keyNavComponent = keyNavComponent;
    }

    public void setInPharmacyReturnMasterGridPanel (InPharmacyReturnMasterGridPanel inPharmacyReturnMasterGridPanel)
    {
        m_inPharmacyReturnMasterGridPanel = inPharmacyReturnMasterGridPanel;
    }

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
